package com.example.howard.xiao7music.service;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Message;

import java.io.IOException;

public class MusicPlayerService extends Service implements IMusicPlayerService, MediaPlayer.OnPreparedListener,
        MediaPlayer.OnBufferingUpdateListener {
    public MediaPlayer mediaPlayer;

    public MusicPlayerService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void callplay(String path) {
        if (mediaPlayer==null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.setLooping(true);
            mediaPlayer.start();

        }else {
            mediaPlayer.reset();

            try {
                mediaPlayer.setDataSource(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer arg0) {
                    mediaPlayer.start();
                }
            });
        }
    }

    @Override
    public void callrestart() {
        if (mediaPlayer!=null){
            mediaPlayer.start();
        }

    }

    @Override
    public void callStop() {
        if(mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
        }
    }

    @Override
    public boolean callIsPlaying() {
        if(mediaPlayer!=null) {
            return mediaPlayer.isPlaying();
        }
        return false;
    }

    @Override
    public int callGetgetDuration() {
        if (mediaPlayer!=null){
            return  mediaPlayer.getDuration();
        }
        return 0;
    }

    @Override
    public int callGetgetCurrentDuration() {
        if (mediaPlayer!=null){
            return mediaPlayer.getCurrentPosition();
        }
        return 0;
    }

    @Override
    public boolean callMediaIsNull() {
        if (mediaPlayer==null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void callChangeSeek(int position) {
        mediaPlayer.seekTo(position);
        if (mediaPlayer.isPlaying()){
            mediaPlayer.start();
        }else{
            mediaPlayer.pause();
        }
    }

    @Override
    public void callPause() {
        mediaPlayer.pause();
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {

    }
}
